package com.briup.myblog.web.controller;


import com.briup.myblog.aop.Logging;
import com.briup.myblog.bean.basic.Carousel;
import com.briup.myblog.service.ICarouselService;
import com.briup.myblog.utils.EntityUtil;
import com.briup.myblog.utils.Result;
import com.briup.myblog.utils.ResultUtil;
import com.briup.myblog.web.vm.CarouselVM;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Api(tags = "轮播图模块")
@RestController
@RequestMapping("/auth/carousel")
public class CarouselController {
    @Autowired  //自动装配IOC容器的接口类型的对象
    private ICarouselService service;

    @Logging
    @ApiOperation("查找所有启用轮播图")
    @GetMapping("/find/yes")
    public Result findAllCarouse(){
        List<Carousel> list = service.findAllCarousel();
        return ResultUtil.success(list);
    }

    @Logging
    @ApiOperation("通过id删除轮播图")
    @ApiImplicitParam(name="id",required = true)
    @DeleteMapping("/deleteById")
    public Result deleteById(Integer id){
        service.deleteById(id);
        return ResultUtil.success("删除成功");
    }

    @Logging
    @ApiOperation("分页查询轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value = "当前页码",required = true),
            @ApiImplicitParam(name="pageSize",value = "页码大小",required = true)
    })
    @GetMapping("/page/query")
    public Result findAll(Integer pageSize, Integer pageNum){
        PageInfo<Carousel> pageInfo = service.findAll(pageNum, pageSize);;
        return ResultUtil.success(pageInfo);
    }

    @Logging
    @ApiOperation("保存轮播图信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name="carouselName",value = "轮播图名称",required = true),
            @ApiImplicitParam(name="carouselUrl",value = "轮播图地址",required = true),
            @ApiImplicitParam(name="carouselInfo",value = "轮播图信息",required = true),
    })
    @PostMapping("/save")
    public Result saveCarousel(CarouselVM vm){
        service.saveCarousel(EntityUtil.vm2Entity(vm,Carousel.class));
        return ResultUtil.success("轮播图保存成功");

    }

    @Logging
    @ApiOperation("修改轮播图信息")
    @ApiImplicitParam(name="carouselId",value = "id",required = true)
    @PutMapping("/update")
    public Result updateCarouse(Carousel vm) {
        service.updateCarousel(vm);
        return ResultUtil.success("轮播图更新成功");
    }

}
